{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Flow Control and Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import scipy.stats as stats\n",
    "from numpy import absolute, ones, zeros\n",
    "from numpy.random import randn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r1 = randn(1)\n",
    "r2 = randn(1)\n",
    "if r1 < 0 and r2 < 0:\n",
    "    pass\n",
    "elif (r1 >= 0 and r2 < 0) or (r1 < 0 and r2 >= 0):\n",
    "    pass\n",
    "else:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 1000\n",
    "e = randn(2 * T)\n",
    "phi1 = 1.4\n",
    "phi2 = -0.8\n",
    "theta1 = 0.4\n",
    "theta2 = 0.8\n",
    "y = zeros(e.shape)\n",
    "for t in range(2, 2 * T):\n",
    "    y[t] = (\n",
    "        phi1 * y[t - 1] + phi2 * y[t - 2] + theta1 * e[t - 1] + theta2 * e[t - 2] + e[t]\n",
    "    )\n",
    "y = y[1000:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "omega = 0.05\n",
    "alpha = 0.05\n",
    "beta = 0.9\n",
    "y = zeros(2 * T)\n",
    "sigma2 = ones(2 * T)\n",
    "for i in range(1, 2 * T):\n",
    "    sigma2[t] = omega + alpha * (y[t - 1] ** 2) + beta * sigma2[t - 1]\n",
    "    y[t] = sigma2[t] * e[t]\n",
    "sigma2 = sigma2[1000:]\n",
    "y = y[1000:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "omega = 0.05\n",
    "alpha = 0.02\n",
    "gamma = 0.07\n",
    "beta = 0.9\n",
    "y = zeros(2 * T)\n",
    "sigma2 = ones(2 * T)\n",
    "for i in range(1, 2 * T):\n",
    "    sigma2[t] = (\n",
    "        omega\n",
    "        + alpha * (y[t - 1] ** 2)\n",
    "        + gamma * ((y[t - 1] < 0) * y[t - 1] ** 2)\n",
    "        + beta * sigma2[t - 1]\n",
    "    )\n",
    "    y[t] = sigma2[t] * e[t]\n",
    "sigma2 = sigma2[1000:]\n",
    "y = y[1000:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = 5\n",
    "x = zeros((M, M))\n",
    "for i in range(M):\n",
    "    for j in range(M):\n",
    "        x[i, j] = i * j\n",
    "y = zeros((M, M))\n",
    "for i, row in enumerate(y):\n",
    "    for j, col in enumerate(row):\n",
    "        y[i, j] = i * j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def invert_normal_cdf(prob):\n",
    "    UB = 10.0\n",
    "    LB = -10.0\n",
    "    cdf = stats.norm(loc=0, scale=1).cdf\n",
    "    distance = 1\n",
    "    while distance > 1e-8:\n",
    "        MP = (UB + LB) / 2\n",
    "        MPc = cdf(MP)\n",
    "        if prob > MPc:\n",
    "            LB = MP\n",
    "        else:\n",
    "            UB = MP\n",
    "        distance = absolute(MPc - prob)\n",
    "    return MP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 8    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "icdf = stats.norm(loc=0, scale=1).ppf\n",
    "\n",
    "print(\"invert_normal_cdf(.01):\")\n",
    "invert_normal_cdf(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"icdf(.01):\")\n",
    "icdf(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"invert_normal_cdf(.5):\")\n",
    "invert_normal_cdf(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"icdf(.5):\")\n",
    "icdf(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"invert_normal_cdf(.975):\")\n",
    "invert_normal_cdf(0.975)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"icdf(.975):\")\n",
    "icdf(0.975)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 9\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = randn(20)\n",
    "y = [val for val in r if val < 0]\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"r[r<0]:\")\n",
    "print(r[r < 0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
